﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LModels.Proxy;
using Models;
using Models.Collections;

using LDataAccess;
using LModels;

namespace LClient
{
    public partial class frmMain : XtraForm
    {
        
        long IDWorkspace = 0;

        public frmMain()
        {
            InitializeComponent();

            // Aggiungo il proxy LIDAR
            AppStaticObjects.PM.AddProxy("lidarDB", new Proxy());

            // Carico la TreeView
            LoadTreeview();
        }

        private void LoadTreeview()
        {
            grdWorkspaces.DataSource = new FactoryCollection<WorkspaceCollection>(AppStaticObjects.PM.GetProxy("lidarDB")).List();
        }

        private void grvWorkspaces_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //Validating selected row
            if (e.RowHandle < 0)
            {
                //Writing code for invalid row selected
            }
            else
            {
                //Models.Workspace workspace = (Models.Workspace)grvWorkspaces.GetRow(e.RowHandle);
                Workspace w = grvWorkspaces.GetRow(e.RowHandle) as Workspace;
                
                RecordingCollection Recordings = new FactoryCollection<RecordingCollection>(AppStaticObjects.PM.GetProxy("lidarDB")).GetInstance();
                grdRecordings.DataSource = Recordings != null ? Recordings.ListById(w.ID) : null;

            }

        }

        private void btnWNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // Workspace
            frmWorkspace frm = new frmWorkspace();
            frm.ShowDialog();

            // Get saved value
            IDWorkspace = frm.IDWorkspace;

            // Form reload e focus
            LoadTreeview();

        }

        private void btnWModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // Workspace
            frmWorkspace frm = new frmWorkspace();
            frm.IDWorkspace = IDWorkspace;
            frm.ShowDialog();

            // Form reload e focus
            LoadTreeview();

        }

        private void btnLidarNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            // Open LIDAR form
            frmLidar frm = new frmLidar();
            frm.IDWorkspace = IDWorkspace;
            frm.ShowDialog();

            // Form reload e focus
            LoadTreeview();

        }

    }

}

